<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelLevels extends JModel
{
	/**
	* Levels data array
	* @var mixed
	*/
	var $_data  = null;

	/**
	 * Levels total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */

	function __construct()
	{
		parent::__construct();

		global $mainframe,$option;

		$limit		= $mainframe->getUserStateFromRequest( $option.'.level.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest( $option.'.level.limitstart', 'limitstart', 0, 'int' );
		
		$limitstart = ( $limit != 0 ? (floor($limitstart / $limit) * $limit) : 0 );

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

	/**
	 * Method to get levels data
	 *
	 * @access public
	 * @return array
	 */
	function getItems() 
	{
		// Lets load the levels if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			//echo $query;
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}

		return $this->_data;
	}

	/**
	 * Method to get the total number of levels
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal() 
	{
		// Lets load the levels if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	/**
	 * Method to get a pagination object for the levles
	 *
	 * @access public
	 * @return object
	 */
	function getPagination()
	{
		// Lets load the level if it doesn't already exist
		if (empty($this->_pagination)) 
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new FMCPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}

	/**
	 * Method to build the query for the levels
	 *
	 * @access private
	 * @return integer
	 * @since 1.0
	*/
	function _buildQuery()
	{
		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildContentWhere();
		$orderby	= $this->_buildContentOrderBy();

		//Query to retrieve all levels.
		$query = ' SELECT  * FROM #__ownservice_levels '
				.$where
				.$orderby;

		return $query;
	}

	/**
	 * Method to build the orderby clause of the query for the levels
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentOrderBy()
	{
		global $mainframe, $option;

		$filter_order 		= $mainframe->getUserStateFromRequest($option.'.level.filter_order', 'filter_order', 'from', 'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($option.'.level.filter_order_Dir', 'filter_order_Dir', 'ASC', 'cmd');

		$orderby = ' ORDER BY `'.$filter_order.'` '.$filter_order_Dir;

		return $orderby;
	}

	/**
	 * Method to build the where clause of the query for the levels
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentWhere()
	{
		global $mainframe, $option;

		$where = array();
		$where = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		return $where;
	}

	/**
	 * Method to remove a level
	 *
	 * @access public
	 * @return boolean true on success
	*/
	function delete($id)
	{
		//Query sql
		$query = " DELETE FROM #__ownservice_levels WHERE id=".$id;

		$this->_db->setQuery($query);
		if(!$this->_db->query()){
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return true;
	}
}